<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时</title>
    <style>
        div{
            margin:0 auto;
           
            width: 900px;
            height: 100px;
        }
       
        #s{
              display: inline-block;

              height: 100px;
              width: 100px;
              text-align: center;
              line-height: 100px;
              border-radius: 50%;
              background-color: rgb(60, 26, 255);
            font-size: 30px;
            color: white;
        }
    </style>
</head>

<body>
    <div><span class="day" id="s"></span>
        <span>天</span>
        <span class="hour" id="s"></span>
        <span>时</span>
        <span class="minute" id="s"></span>
        <span>分</span>
        <span class="second" id="s"></span>
        <span>秒</span></div>
    
        <button class="begin">开启定时器</button>
        <button class="stop">停止定时器</button>
    <script>
        // function countDown(time) {

        //     var nowTime = +new Date();
        //     var inputTime = +new Date(time);
        //     var times = inputTime - nowTime;
        //     var times = (inputTime - nowTime) / 1000;

        //     var d = parseInt(times / 60 / 60 / 24); //天
        //     d = d < 10 ? "0" + d : d;
        //     var h = parseInt(times / 60 / 60 % 24); //分 h=h<10?"0"+h:h; var m=parseInt(times/60/60);//分
        //   h= h < 10 ? "0" + h : h;
        //     var m = parseInt(times / 60 %60);
        //     m = m < 10 ? "0" + m : m;
        //     var s = parseInt(times % 60); //分 s=s<10?"0"+s:s; return d+'天'+h+"时"+m+'分'+s+'秒'; }
        //      s = s < 10 ? "0" + s : s;
        //      return d+'天'+h+'时'+m+'分'+s+'秒';
        // }
        // document.write(countDown('2022-2-15 18:00:00'));

        // 开启关闭定时器
        var begin=document.querySelector(".begin");
        var stop=document.querySelector(".stop");
        

        var timer=null;//定义一个全局变量 null是一个空变量
        begin.addEventListener('click',function(){
            timer=  setinterval(function(){
              alert('继续学习吧')
            },1000)
        })

        stop.addEventListener ('click',function(){
            clearInterval(timer)
        })


        var day = document.querySelector('.day'); //获取天数盒子
        var hour = document.querySelector('.hour'); //获取小时盒子
        var minute = document.querySelector('.minute'); //获取分钟盒子
        var second = document.querySelector('.second'); //获取秒数盒子
        var inputTime = +new Date("2022-2-15 18:00:00"); //用户输入时间总毫秒数，即输入用户截止时间，从多会开始倒计时
        //    第二部，每隔多长时间调用
        setInterval(countDown, 1000); //每隔一秒调用这个函数
        function countDown() {

            var nowTime = +new Date();

            var times = inputTime - nowTime;
            var times = (inputTime - nowTime) / 1000;

            var d = parseInt(times / 60 / 60 / 24); //天
            day.innerHTML = d;
            d = d < 10 ? "0" + d : d;
            var h = parseInt(times / 60 / 60 % 24); //分 h=h<10?"0"+h:h; var m=parseInt(times/60/60);//分
            h = h < 10 ? "0" + h : h;
            hour.innerHTML = h; //把剩余的小时给到小盒子里
            var m = parseInt(times / 60 % 60);
            m = m < 10 ? "0" + m : m;
            minute.innerHTML = m; //把剩余的分钟给到小盒子里
            var s = parseInt(times % 60); //分 s=s<10?"0"+s:s; return d+'天'+h+"时"+m+'分'+s+'秒'; }
            s = s < 10 ? "0" + s : s;
            second.innerHTML = s; //把剩余的秒给到小盒子里

        }
    </script>
</body>

</html>